package com.csx.micrometer.discovery.timer;

import com.csx.micrometer.discovery.adapter.RegistryCenterServiceAdapter;
import com.csx.micrometer.discovery.model.HttpServiceDiscoveryVO;
import com.csx.micrometer.discovery.model.InstanceDetailPojo;
import com.csx.micrometer.discovery.model.NamespaceDetail;
import com.csx.micrometer.discovery.model.ServicePojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cuisongxu
 * @date 2022/12/5 14:19
 */
@Component
public class HttpServiceDiscoveryTask {

    private List<HttpServiceDiscoveryVO> serviceDiscoveryVOList = new ArrayList<>();

    @Autowired
    private RegistryCenterServiceAdapter registryCenterServiceAdapter;

    // TODO 考虑如何自定义线程的延迟时间
    @Scheduled(initialDelay = 10000, fixedDelay = 10000)
    public void scheduledServiceDiscovery() {

        List<HttpServiceDiscoveryVO> newServiceDiscoveryList = new ArrayList<>();

        // 查询所有的命名空间
        List<NamespaceDetail> namespaces = registryCenterServiceAdapter.getNamespaces();
        for (NamespaceDetail namespace : namespaces) {

            // 查询所有服务
            List<ServicePojo> services = registryCenterServiceAdapter.getServices(namespace);
            for (ServicePojo service : services) {

                // 查询所有实例
                List<InstanceDetailPojo> instances = registryCenterServiceAdapter.getInstances(namespace.getNamespace(), service);

                for (InstanceDetailPojo instance : instances) {
                    Map<String, Object> metadata = instance.getMetadata();
                    if (metadata.containsKey("metrics.enable") && "true".equals(metadata.get("metrics.enable"))) {
                        List<String> hostList = new ArrayList<>();
                        // 填充标签
                        Map<String, String> labels = new HashMap<>();
                        labels.put("__meta_name", instance.getServiceName());
                        labels.put("__meta_endpoint", (String) metadata.get("prometheusEndpoints"));
                        labels.put("__meta_zoneId", (String) metadata.get("zoneId"));
                        labels.put("__meta_unitId", (String) metadata.get("unitId"));
                        labels.put("__meta_systemId", (String) metadata.get("systemId"));
                        labels.put("__meta_envId", (String) metadata.get("envId"));
                        labels.put("__meta_region", (String) metadata.get("region"));

                        hostList.add(instance.getIp() + ":" + instance.getPort());
                        HttpServiceDiscoveryVO discoveryVO = new HttpServiceDiscoveryVO();
                        discoveryVO.setTargets(hostList);
                        discoveryVO.setLabels(labels);
                        newServiceDiscoveryList.add(discoveryVO);
                    }
                }
            }
            serviceDiscoveryVOList = newServiceDiscoveryList;
        }
    }

    public List<HttpServiceDiscoveryVO> getServiceDiscoveryVOList() {
        return serviceDiscoveryVOList;
    }
}
